package com.ruoyi.system.monitortask.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoMapper;
import com.ruoyi.system.V2.goodInfo.service.IGoodsInfoService;
import com.ruoyi.system.monitortask.domain.MonitorTaskErr;
import com.ruoyi.system.monitortask.domain.MonitorTaskRecord;
import com.ruoyi.system.monitortask.domain.MonitoringErr;
import com.ruoyi.system.monitortask.mapper.MonitorTaskErrMapper;
import com.ruoyi.system.monitortask.mapper.MonitorTaskGoodsMapper;
import com.ruoyi.system.monitortask.mapper.MonitorTaskRecordMapper;
import com.ruoyi.system.monitortask.mapper.MonitoringErrMapper;
import com.ruoyi.system.monitortask.service.IMonitorTaskErrService;
import com.ruoyi.system.monitortask.service.IMonitorTaskRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 账号监测查询记录表(monitor_task_record)表服务实现类
 *
 * @author zyy
 * @date 2024-06-22 14:48:25
 */
@Service("monitorTaskRecordService")
public class MonitorTaskRecordServiceImpl extends ServiceImpl<MonitorTaskRecordMapper, MonitorTaskRecord> implements IMonitorTaskRecordService {

    @Autowired
    MonitoringErrMapper monitoringErrMapper;
    @Autowired
    IGoodsInfoService goodsInfoService;
    @Autowired
    GoodsInfoMapper goodsInfoMapper;
    @Autowired
    MonitorTaskGoodsMapper monitorTaskGoodsMapper;
    @Autowired
    MonitorTaskErrMapper monitorTaskErrMapper;
    @Autowired
    IMonitorTaskErrService monitorTaskErrService;

    @Autowired
    MonitorTaskRecordMapper monitorTaskRecordMapper;

//    String backStr = "{key=147258369, {\"type\":\"check\",\"checkId\":1601157,\"qq\":\"135245138\"," +
//            "\"telephone\":\"18530912310\",\"status\":\"0\", \"queryResult\":\"185******10\"}=}";

    /**
     * @param backStr
     */
    @Override
    public void callBack(String backStr) {
        JSONObject jsonObject = JSONObject.parseObject(backStr);
        String type = jsonObject.getString("type");
        String qq = jsonObject.getString("qq");
        String checkId = jsonObject.getString("taskId");
        String telephone = jsonObject.getString("telephone");
        String status = jsonObject.getString("status");
        String queryResult = jsonObject.getString("queryResult");

        LambdaQueryWrapper<MonitorTaskRecord> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(MonitorTaskRecord::getCheckId, checkId);
        MonitorTaskRecord monitorTaskRecord = getOne(queryWrapper);
        if (Objects.isNull(monitorTaskRecord)) {
            MonitoringErr monitoringErr = new MonitoringErr();
            monitoringErr.setErrText("进入了账号监测回调,但是未找到监测记录,请管理员检查,checkId：" + checkId);
            monitoringErrMapper.insert(monitoringErr);
            return;
        }
        monitorTaskRecord.setQq(qq);
        monitorTaskRecord.setTelephone(telephone);
        monitorTaskRecord.setStatus(status);
        monitorTaskRecord.setQueryId(checkId);
        monitorTaskRecord.setBindTel(queryResult);
        monitorTaskRecord.setTaskStatus("2");
        monitorTaskRecord.setTaskCompleteTime(DateUtils.getTime());
        updateById(monitorTaskRecord);

        //处理商品逻辑

        Long goodsId = monitorTaskRecord.getGoodsId();
        Long monitorTaskId = monitorTaskRecord.getMonitorTaskId();
        Integer monitorTaskType = monitorTaskRecord.getMonitorTaskType();

        if (StringUtils.equals("0", status)) {
            //正常
            goodsInfoMapper.updateMonitoringSome(goodsId, "2", "1");
        } else if (StringUtils.equals("1", status)
                || StringUtils.equals("2", status)
                || StringUtils.equals("4", status)) {
            //已变更
            String monitoringState = "2";
            if (monitorTaskType.intValue() == 1) {
                monitoringState = "3";
                monitorTaskGoodsMapper.deleteByMonitoTaskIdAndGoodsId(monitorTaskId, goodsId);
            }
            goodsInfoMapper.updateMonitoringSome(goodsId, monitoringState, "2");
            MonitorTaskErr monitorTaskErr = new MonitorTaskErr();
            monitorTaskErr.setMonitorTaskId(monitorTaskId);
            monitorTaskErr.setMonitorTaskType(monitorTaskType);
            monitorTaskErr.setGoodsId(goodsId);
            monitorTaskErr.setRecordId(monitorTaskRecord.getId());
            monitorTaskErrService.save(monitorTaskErr);
        } else if (StringUtils.equals("3", status)) {
            //今日上限
        }
    }

    @Override
    public List<MonitorTaskRecord> selectList(MonitorTaskRecord monitorTaskRecord) {
        return monitorTaskRecordMapper.selectMonitorTaskRecordList(monitorTaskRecord);
    }
}

